# -*- coding: utf-8 -*-

"""
Date: 2023/2/27 11:41
Author: Darker
"""
import sqlite3
from typing import Text, Tuple, Optional, List, Any


class MySqlite(object):

    def __init__(self):
        self.conn: sqlite3.Connection
        self.cursor: sqlite3.Cursor

    def __enter__(self):
        self.__create_connection()
        self.__create_cursor()

    def __create_connection(self):
        """创建内存数据库连接对象"""
        self.conn = sqlite3.connect(":memory:")

    def __create_cursor(self):
        self.cursor = self.conn.cursor()

    def execute(self, sqlText: Text) -> list[Any]:
        """执行sql语句"""
        self.cursor.execute(sqlText)
        self.conn.commit()
        return self.cursor.fetchall()

    def execute_many(self, sqlList: List) -> Optional[List]:
        """执行多条sql语句"""
        result = []
        for sql in sqlList:
            result.extend(self.execute(sql))
        return result

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.cursor.close()
        self.conn.close()


mySqlite = MySqlite()
